🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

svg-to-vue-component

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svg-to-vue-component

Compile SVG files to Vue SFC.

0.2.6
Source
npm
Version published
Weekly downloads
5.2K
16.88%
Maintainers
1
Weekly downloads
 
Created
Source

svg-to-vue-component

NPM version NPM downloads CircleCI donate chat

Why

When you import the .svg file as a Vue component instead of using the URL to the file, you can style it with CSS and add addtional DOM properties or event handlers to the component directly.

The differences between this project and vue-svg-loader are:

  • This one has built-in hot reloading support for webpack since the SVG code is compiled via vue-loader.
  • The latter only supports class and style attributes on the generated component while we support all DOM props and events.
  • This one supports project-wise and file-relative configuration for SVGO.

Install

yarn add svg-to-vue-component --dev

Usage

With Webpack

// webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.svg$/,
        use: [
          // This loader compiles .svg file to .vue file
          // So we use `vue-loader` after it
          'vue-loader',
          'svg-to-vue-component/loader'
        ]
      }
    ]
  },
  // ...other configurations
}

Then you can import .svg files directly and use them as Vue functional components:

<template>
  <!-- Dom props and events are all available here -->
  <CheckIcon width="20px" height="20px" @click="handleClick" />
</template>

<script>
import CheckIcon from './check-icon.svg'

export default {
  components: {
    CheckIcon
  },

  methods: {
    handleClick() {
      console.log('It works!')
    }
  }
}
</script>

With Vue CLI or Poi

In your vue.config.js or poi.config.js:

module.exports = {
  chainWebpack(config) {
    // Remove existing SVG rule which uses file-loader
    config.module.rules.delete('svg')

    // Use our loader instead
    config.module.rule('svg')
      .test(/\.svg$/)
      .use('vue')
      .loader('vue-loader')
        .end()
      .use('svg-to-vue-component')
      .loader('svg-to-vue-component/loader')
  }
}

Nuxt.js 2

In your nuxt.config.js:

module.exports = {
  build: {
    extend(config) {
      const imageLoaderRule = config.module.rules.find(
        rule => rule.test && rule.test.test('.svg')
      )
      if (!imageLoaderRule) {
        throw new Error('Cannot find the existing webpack rule for .svg files')
      }
      
      // Don't process .svg files in default image rule
      // from https://github.com/nuxt/nuxt.js/blob/76b10d2d3f4e5352f1c9d14c52008f234e66d7d5/lib/builder/webpack/base.js#L203
      imageLoaderRule.test = /\.(png|jpe?g|gif|webp)$/

      // Add a new rule for .svg file
      config.module.rules.push({
        test: /\.svg$/,
        use: ['vue-loader', 'svg-to-vue-component/loader']
      })
    },
  },
};

Loader Options

Pass loader options like this:

// ...
{
  test: /\.svg$/,
  use: [
    'vue-loader',
    {
      loader: 'svg-to-vue-component/loader',
      options: {
        // ...Your options here
      }
    }
  ]
}
OptionDescription
svgoConfigProject-wise configuration for SVGO, if you want file-relative configuration, use the config file instead, supported format: .svgo.{yml,js,json}, see here for an example file. If you want to turn off SVGO entirely, pass false here.

Contributing

  • Fork it!
  • Create your feature branch: git checkout -b my-new-feature
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request :D

Author

svg-to-vue-component © EGOIST, Released under the MIT License.
Authored and maintained by EGOIST with help from contributors (list).

Website · GitHub @EGOIST · Twitter @_egoistlily

FAQs

Package last updated on 06 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts